﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;
using System.Data.SqlClient;
namespace IHISCETeim.DAL
{
    public class UClaimDemageControlOITMDetailDAL
    {
        public static bool AddUClaimDemageControlOITMDetail(int UCDCID, List<UClaimDemageControlOITMDetailInfo> list)
        {


            Database DB = DatabaseFactory.CreateDatabase();

           // DbCommand dbCommand = DB.GetStoredProcCommand("UClaimDemageControlOITMDetail_AddUClaimDemageControlOITMDetail", UCDCID, ItemCode, ItemName, Quantity);
           // return Convert.ToInt32(DB.ExecuteScalar(dbCommand));

            using (DbConnection conn = DB.CreateConnection())
            {
                //打开连接
                conn.Open();
                //创建事务
                DbTransaction trans = conn.BeginTransaction();
                try
                {

                    //1.删除
                    DbCommand objReceivablesdbCommand = DB.GetStoredProcCommand("UClaimDemageControlOITMDetail_DeleteByUCDCID", UCDCID);
                    DB.ExecuteNonQuery(objReceivablesdbCommand, trans);
                    
                    //2.添加

                   
                    //3.应收账款帐龄明细表(FTradeAgeDetail)
                    foreach (UClaimDemageControlOITMDetailInfo objDetail in list)
                    {
                        DbCommand dbCommand = DB.GetStoredProcCommand("UClaimDemageControlOITMDetail_AddUClaimDemageControlOITMDetail", UCDCID, objDetail.ItemCode, objDetail.ItemName, objDetail.Quantity);
                        int id = Convert.ToInt32(DB.ExecuteScalar(dbCommand, trans));
                    }

                    trans.Commit();
                    return true;
                }
                catch (Exception exc)
                {
                    //发生异常，事务回滚
                    trans.Rollback();
                    return false;
                }
                finally
                {
                    //关闭连接
                    conn.Close();
                }
            }

        }
        public static bool DeleteByUCDCID(int UCDCID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UClaimDemageControlOITMDetail_DeleteByUCDCID", UCDCID);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static IDataReader GetListByUCDCID(int UCDCID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UClaimDemageControlOITMDetail_GetListByUCDCID", UCDCID);
            return DB.ExecuteReader(dbCommand);
        }
    }
}
